package cn.maidouya.order.controller;
import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.basic.util.LoginContext;
import cn.maidouya.basic.util.PageList;
import cn.maidouya.order.domain.AdoptOrder;
import cn.maidouya.order.query.AdoptOrderQuery;
import cn.maidouya.order.service.IAdoptOrderService;
import cn.maidouya.order.service.IProductOrderService;
import cn.maidouya.user.domain.Logininfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/adoptOrder")
public class AdoptOrderController {

    public static final String 后台领养订单详情页接口 = "后台领养订单详情页接口";
    @Autowired
    private IAdoptOrderService adoptOrderService;

    /**
     *    pet领养订单
     * @param params 订单信息
     * @param request 当前登录人
     * @return
     */
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){
        System.out.println("============="+params+"--------------------" +LoginContext.getLogininfo(request));
        try{
            String payData = adoptOrderService.submit(params, LoginContext.getLogininfo(request));
            System.out.println(payData);
            return AjaxResult.getResult().setResultObj(payData);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("下单失败！"+e.getMessage());
        }
    }
    // 后台：平台和店铺管理员查询订单
    @PostMapping("/admin")
    public PageList<AdoptOrder> admin(@RequestBody AdoptOrderQuery query,HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return adoptOrderService.admin(query,logininfo.getId());
    }
    // 前台：用户查询订单
    @PostMapping("/user")
    public PageList<AdoptOrder> user(@RequestBody AdoptOrderQuery query, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return adoptOrderService.user(query,logininfo.getId());
    }

    // 后台领养订单详情
    @GetMapping("/{id}")
    @ApiOperation(value = "后台领养订单详情页接口",notes ="后台领养订单详情页接口" )
    public AdoptOrder adoptDetail(@PathVariable Long id){
       return adoptOrderService.loadById(id);
    }
}